package at.ac.univie.ise.generator;

import java.sql.Time;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import at.ac.univie.ise.common.Config;
import at.ac.univie.ise.common.Constants;
import at.ac.univie.ise.schema.enums.StationhoursDay;
import at.ac.univie.ise.schema.tables.records.StationhoursRecord;
import de.pyranja.util.Utils;

public class EntityComponents {
	
	// singleton
	private static EntityComponents instance = new EntityComponents();

	public static EntityComponents getInstance() {
		return instance;
	} // endof singleton ------------------------------------------------>
	
	private Random rng;
	private int licenceCounter;
	
	private List<String> streetPrefix;
	private List<String> streetSuffix;
	
	private List<String> maleFirst;
	private List<String> femaleFirst;
	private List<String> last;
	
	private List<String> licences;
	
	private List<String> stationQualifier;
	
	protected EntityComponents() {
		Config config = Config.getInstance();
		this.rng = config.getRng();
		licenceCounter = Utils.rangedRandomInt(Constants.LICENCE_MIN, Constants.LICENCE_MAX, rng);
		// street
		String nameBundle = config.getConfig().getProperty(Constants.BUNDLEBASE) + ".names";
		streetPrefix = Utils.loadFromBundle(nameBundle, Constants.STREET_PREFIX);
		Collections.shuffle(streetPrefix, rng);
		streetSuffix = Utils.loadFromBundle(nameBundle, Constants.STREET_SUFFIX);
		Collections.shuffle(streetSuffix, rng);
		// names
		maleFirst = Utils.loadFromBundle(nameBundle, Constants.NAME_MALE);
		Collections.shuffle(maleFirst, rng);
		femaleFirst = Utils.loadFromBundle(nameBundle, Constants.NAME_FEMALE);
		Collections.shuffle(femaleFirst, rng);
		last = Utils.loadFromBundle(nameBundle, Constants.NAME_LAST);
		Collections.shuffle(last, rng);
		licences = Utils.loadFromBundle(nameBundle, Constants.LICENCES);
		stationQualifier = Utils.loadFromBundle(nameBundle, Constants.STATION_QUALIFIER);
	}
	
	public List<String> getLicences() {
		return licences;
	}
	
	public String concatEmail(String prefix) {
		return prefix + "@test.de";
	}
	
	public int licenceNumber() {
		licenceCounter += rng.nextInt(Constants.LICENCE_STEP) + 1;
		return licenceCounter;
	}
	
	public String randomLicence() {
		return Utils.randomElem(licences, rng);
	}
	
	public String randomQualifier() {
		return Utils.randomElem(stationQualifier, rng);
	}
	
	public String randomStreet() {
		StringBuilder sb = new StringBuilder();
		sb.append(Utils.randomElem(streetPrefix,rng));
		sb.append(Utils.randomElem(streetSuffix,rng));
		sb.append(" ");
		sb.append(rng.nextInt(Constants.MAX_NR) + 1);
		return sb.toString();
	}
	
	public String salut(boolean male) {
		if (male) {
			return Constants.MALE_SALUT;
		} else {
			return Constants.FEMALE_SALUT;
		}
	}
	
	public boolean randomSex() {
		return rng.nextBoolean();
	}
	
	public String randomFirstName(boolean male) {
		if ( male ) {
			return Utils.randomElem(maleFirst,rng);
		} else {
			return Utils.randomElem(femaleFirst,rng);
		}
	}
	
	public String randomLastName() {
		return Utils.randomElem(last,rng);
	}
	
	public String randomPhone() {
		return Utils.randomNumber(rng,10);
	}
	
	public StationhoursRecord defHours(StationhoursRecord fillMe, StationhoursDay day) {
		fillMe.setDay(day);
		fillMe.setOpen(Time.valueOf("08:00:00"));
		fillMe.setClose(Time.valueOf("19:00:00"));
		return fillMe;
	}
}
